package com.flower.controller;

import com.flower.pojo.Flower;
import com.flower.pojo.Imgs;
import com.flower.service.FlowerService;
import com.flower.service.ImgsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/flower")
public class FlowerController {

    @Autowired
    private FlowerService flowerService;
    @Autowired
    private ImgsService imgsService;

    @RequestMapping("/add_flower.do")
    public Map<String, Object> addFlower(Flower flower, @RequestParam(value = "imgCover") MultipartFile multipartFile, @RequestParam(value = "imgCovers") MultipartFile[] multipartFiles, HttpServletRequest request) throws IOException {

        Map<String, Object> map = new HashMap<>();
        String path = request.getServletContext().getRealPath("/upload");
        File dirPath = new File(path);
        if (!dirPath.exists()) {
            dirPath.mkdirs();
        }
        File file = new File(dirPath, multipartFile.getOriginalFilename());
        multipartFile.transferTo(file);
        flower.setCover("upload/" + file.getName());
        List<Imgs> imgsList = new ArrayList<>();
        for (MultipartFile multipartFile1 : multipartFiles) {
            File files = new File(dirPath, multipartFile1.getOriginalFilename());
            multipartFile1.transferTo(files);
            Imgs imgs = new Imgs();
            imgs.setUrl("upload/" + files.getName());
            imgsList.add(imgs);
        }
        try {
            int result = flowerService.addFlower(flower, imgsList);
            if (result != -1) {
                map.put("code", 200);
                map.put("msg", "上传成功");
            }
        } catch (Exception e) {
            map.put("code", 201);
            map.put("msg", "上传失败");
        }
        return map;
    }

    @RequestMapping("/query_store_flower.do/{sid}")
    public Map<String, Object> queryStoreFlower(@PathVariable("sid") Integer sid,Integer currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage,3);
        List<Flower> list = flowerService.queryStoreFlower(sid);
        PageInfo<Flower> pageInfo=new PageInfo<>(list);
         int total = (int) pageInfo.getTotal();
         int pageCount=pageInfo.getPages();
        if (list != null) {
            map.put("code", 200);
            map.put("msg", "查询成功");
            map.put("data", list);
            map.put("total",total);
            map.put("pageCount",pageCount);
        } else {
            map.put("code", 201);
            map.put("msg", "查询失败");
        }
        return map;
    }

}
